package controller;

import bean.HomeData;
import bean.Users;
import service.CommodityService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/HomeController")
public class HomeController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String keyword = req.getParameter("keyword");
        int sort=req.getParameter("sort")==null?0:Integer.parseInt(req.getParameter("sort"));
        int curPage=req.getParameter("curPage")==null?1:Integer.parseInt(req.getParameter("curPage"));
        HomeData homeData=new HomeData(keyword,sort,curPage);
        CommodityService.prepareForHome(homeData);

        Users user=(Users) req.getSession().getAttribute("user");
        if (user!=null){
            req.getSession().setAttribute("user", user);
        }

        req.setAttribute("homeData",homeData);
        req.getRequestDispatcher("home.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
